<?php

namespace Beerus\Modules\VirtualPhone\Actions;

use Encore\Admin\Actions\RowAction;
use Illuminate\Http\Request;
use Doraemon\model\VirtualPhone\DesignerVirtualPhoneRecord;
use Doraemon\tools\AlibabaCloud\VirtualPhone;

class ReleaseVirtualPhone extends RowAction
{
    public $name = '解绑号码';

    public function handle($id)
    {
        //调用阿里云侧释放双方号码
        $virtualInfo = DesignerVirtualPhoneRecord::getByCondition(['id' => $id]);
        $subsId = $virtualInfo['subs_id'];
        $virtualPhone = $virtualInfo['virtual_phone'];
        $virtualExpireTime = $virtualInfo['virtual_expire_time'];
        $nowTime = date("Y-m-d H:i:s");
        if ($virtualExpireTime >= $nowTime){
            //未过期的号码才可以释放号码
            VirtualPhone::ReleaseVirtualPhone($subsId,$virtualPhone);
        }
        //修改数据库中数据状态为删除
        DesignerVirtualPhoneRecord::update(["data_status" => DesignerVirtualPhoneRecord::STATUS_DELETE],['id' => $id]);

        return $this->response()->success('解绑成功')->refresh();
    }

    public function retrieveModel(Request $request)
    {
        if (!$key = $request->get('_key')) {
            return false;
        }
        return $key;
    }

    public function dialog()
    {
        $this->confirm('确定解绑该号码？');
    }
}
